package com.example.websocket.phone;

public class SynFun extends Thread {

    private int num = 10;

    @Override
    public void run() {
        try {
            this.fun();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

//    public synchronized void fun() throws InterruptedException {
//        Thread.sleep(3000);
//        System.out.println("修改前的num为" + num);
//        num--;
//        System.out.println("修改后的num为" + num);
//        System.out.println("***************************");
//    }

    public void fun() throws InterruptedException {
        Thread.sleep(3000);
        synchronized (this) {
            System.out.println("修改前的num为" + num);
            num--;
            System.out.println("修改后的num为" + num);
        }
        System.out.println("***************************");
    }

    public static void main(String[] args) {
        SynFun synFun = new SynFun();
        Thread t1 = new Thread(synFun);
        Thread t2 = new Thread(synFun);
        t1.start();
        t2.start();
    }
}
